package com.thread.exector;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class SceduledExecutorServiceTest {

	private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	public static void main(String[] args) {
		System.out.println("开始执行时间：" + sdf.format(new Date()));
		ScheduledExecutorService executorService = Executors.newScheduledThreadPool(5);
		executorService.scheduleAtFixedRate(new SceduledExecutorServiceTest.FixedRatedTask(), 5, 3, TimeUnit.SECONDS);

		executorService.scheduleWithFixedDelay(new SceduledExecutorServiceTest.FixedDelayTask(), 5, 3,
				TimeUnit.SECONDS);

	}

	static class FixedRatedTask implements Runnable {
		@Override
		public void run() {
			try {
				System.out.println("--当前时间：" + sdf.format(new Date()));
				Thread.sleep(2000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}

		}
	}

	static class FixedDelayTask implements Runnable {
		@Override
		public void run() {
			try {
				System.out.println("==当前时间：" + sdf.format(new Date()));
				Thread.sleep(2000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}

		}

	}
}
